import { queryPage } from './service';

export default {
  namespace: 'download',

  state: {
    list: { data: [] },
  },

  effects: {
    *initCond(_, { put }) {
      yield put({ type: 'setCond', payload: { pageNum: 1  } });
      yield put({ type: 'queryPage' })
    },
    *queryPage({ payload = { pageNum: 1 } }, { call, put, select }) {
      const params = payload;
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryPage, params);
      yield put({ type: 'setPage', payload: { list, total: +total } })
    },
    *updateCond({ payload }, { put, select }) {
      const cond = yield select(state => state.fireroom.cond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setCond', payload: params });
      yield put({ type: 'queryPage' })
    },
  },

  reducers: {
    setPage(state, action) {
      return {
        ...state,
        list: action.payload
      }
    },
    setCond(state, action) {
      return {
        ...state,
        cond: action.payload
      }
    },
  }
}
